package com.neutech.maplestory.client;

import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.entity.*;
import com.neutech.maplestory.util.ImageUtil;
import com.neutech.maplestory.util.MapleStoryFrame;
import com.neutech.maplestory.util.MusicThread;


import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 主函数类
 */
public class MapleStoryClient extends MapleStoryFrame {
    public Hero hero = new Hero(this, 300, 430);
//    public Boss boss = new Boss(this,1200,300);
    //    public Arrow arrow = new Arrow(400,500, Direction.RIGHT);
    public ItemPackage itemPackage = new ItemPackage(this);
    public List<Arrow> arrowList = new CopyOnWriteArrayList<>();
    public List<BossArrow> bossArrows = new CopyOnWriteArrayList<>();
    public List<Item> itemList = new CopyOnWriteArrayList<>();
    public List<Power> powers = new CopyOnWriteArrayList<>();
    public List<Cloud> clouds = new CopyOnWriteArrayList<>();
    public List<Mob> mobs = new CopyOnWriteArrayList<>();
    public List<Boss> bosses = new CopyOnWriteArrayList<>();
    public List<Ground> grounds = new CopyOnWriteArrayList<>();
    public static Image image;
    static{
        image = ImageUtil.getImage("background_map");
    }
    public int num = 1;

     {

        Cloud cloud0 = new Cloud(this,1900,100,10);
        clouds.add(cloud0);
         Cloud cloud1 = new Cloud(this,1900,150,10);
         clouds.add(cloud1);
         Cloud cloud2 = new Cloud(this,1900,50,15);
         clouds.add(cloud2);
         Cloud cloud3 = new Cloud(this,1900,200,8);
         clouds.add(cloud3);
         Cloud cloud4 = new Cloud(this,1900,180,9);
         clouds.add(cloud4);
         Cloud cloud5 = new Cloud(this,1900,120,3);
         clouds.add(cloud5);
    }

    {
        Random random = new Random();


        for (int i = 0; i < 10; i++) {
            Mob mob = new Mob(this, 700 + (i * 120), 530/*Constant.GAME_WINDOW_HEIGHT  - 60*/, 5);
            mobs.add(mob);
        }

            Boss boss = new Boss(this,1200,300);
            bosses.add(boss);

        // todo 怪我数量的设置
    }
    {

        Ground ground01 = new Ground(this,0,590,605,300);//第一个陆地
        grounds.add(ground01);
        Ground ground02 = new Ground(this,110,190,290,2);//左上角第一个矩形
        grounds.add(ground02);
        Ground ground03 = new Ground(this,430,350,300,2);//左上角第二个矩形
        grounds.add(ground03);
        Ground ground04 = new Ground(this,765,250,235,2);//第三个矩形
        grounds.add(ground04);
        Ground ground05 = new Ground(this,1215,310,195,2);//第四个矩形
        grounds.add(ground05);
        Ground ground06 = new Ground(this,1490,190,420,2);//第五个矩形
        grounds.add(ground06);
        Ground ground07 = new Ground(this,605,540,170,350);//第二个陆地
        grounds.add(ground07);
        Ground ground08 = new Ground(this,810,635,80,30);//第一个小台阶
        grounds.add(ground08);
        Ground ground09 = new Ground(this,990,750,80,30);//第二个小台阶
        grounds.add(ground09);
        Ground ground10 = new Ground(this,815,850,80,30);//第三个小台阶
        grounds.add(ground10);
        Ground ground11 = new Ground(this,976,920,80,2);//第四个小台阶
        grounds.add(ground11);
        Ground ground12 = new Ground(this,1080,540,170,350);//第三个陆地
        grounds.add(ground12);
        Ground ground13 = new Ground(this,1250,590,336,300);//第四块陆地
        grounds.add(ground13);
        Ground ground14 = new Ground(this,1585,440,90,450);//第五块陆地
        grounds.add(ground14);
        Ground ground15 = new Ground(this,1675,610,241,280);//第六块陆地
        grounds.add(ground15);
        Ground ground16 = new Ground(this,0,1000 ,1900,2);//最下面的陆地
        grounds.add(ground16);
    }

    /**
     * 重写paint方法
     *
     * @param g 画笔
     */
    @Override
    public void paint(Graphics g) {
        Iterator<Ground> itGrounds = grounds.iterator();
        while (itGrounds.hasNext()){
            Ground ground = itGrounds.next();
            ground.draw(g);
        }

        g.drawImage(image,0,0,null);
        Font font = g.getFont();
        g.setFont(new Font("微软雅黑", Font.BOLD, 40));
        g.drawString("当前boss的方向" + hero.dir, 100, 50);
        g.drawString("当前boss的动作" + hero.action, 100, 100);
        g.drawString("弓箭的数量" + arrowList.size(), 100, 150);
//        g.drawString("当前人物的HP" + hero.HP, 100, 200);
//        g.drawString("当前人物的Mp" + hero.Mp, 100, 250);
        g.drawString("当前怪物的状态" , 100, 300);
        g.drawString("英雄的x坐标为" + hero.x + "英雄的y坐标" + hero.y ,100,350);
        g.drawString("num" + num,100,400);
        g.setFont(font);
        hero.draw(g);
        hero.hit(mobs);


//        arrow.draw(g);

        /**
         * 遍历弓箭然后画出来
         */
        Iterator<Arrow> itArrows = arrowList.iterator();
        while (itArrows.hasNext()) {
            Arrow arrow = itArrows.next();
            arrow.draw(g);
            arrow.hit(mobs);
            arrow.hitBoss(bosses);
        }
        /**
         * 遍历boss
         */
        Iterator <Boss> bossIterator = bosses.iterator();
        while (bossIterator.hasNext()){
            Boss boss = bossIterator.next();
            boss.draw(g);
        }
        /**
         * 遍历boss的技能然后画出来
         */
        Iterator<BossArrow> itBossArrows = bossArrows.iterator();
        while (itBossArrows.hasNext()){
            BossArrow bossArrow = itBossArrows.next();
            bossArrow.draw(g);
            bossArrow.hit(hero);
        }
        /**
         * 遍历云彩画出来
         */
        Iterator<Cloud> cloudIterator = clouds.iterator();
        while (cloudIterator.hasNext()){
            Cloud cloud = cloudIterator.next();
            cloud.draw(g);
        }
        /**
         * 遍历怪物，然后画出来
         */
        Iterator<Mob> itMobs = mobs.iterator();
        while (itMobs.hasNext()) {
            Mob mob = itMobs.next();
            mob.draw(g);
        }
        /**
         * 遍历道具，然后画出来
         */
        Iterator<Item> itItems = itemList.iterator();
        while (itItems.hasNext()) {
            Item item = itItems.next();
            item.draw(g);
        }
        /**
         * 画出伤害值
         */
        Iterator<Power> itPower = powers.iterator();
        while (itPower.hasNext()) {
            Power power = itPower.next();
            power.draw(g);
        }

        /**
         * 画背包
         */
        itemPackage.draw(g);
        /**
         * 遍历矩形，给他画出来
         */


    }

    /**
     * 重写init方法
     * 重写键盘监听方法，，调用hero中具体的键盘动作
     * 为了初始化键盘监听
     */
    @Override
    public void init() {
        super.init();
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_I) {
                    itemPackage.live = !itemPackage.live;
                }

                for (int i = 0; i < itemPackage.packItemList.size(); i++) {
                    /**
                     * 遍历背包，调用吃背包里面的道具方法
                     */
                    hero.keyPress(e, itemPackage.packItemList.get(i));
                }
                if (hero.fly) {
                    hero.keypress(e);
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                hero.keyReleased(e);
                if (hero.fly){
                    hero.keyRelease(e);
                }

            }
        });
        new MusicThread("MapleLeaf.mp3", true).start();//  todo 音乐
    }

    /**
     * 主函数
     *
     * @param args
     */
    public static void main(String[] args) {
        new MapleStoryClient();
    }
}
